// page/dizhi/dizhi.js
Page({

    /**
     * 页面的初始数据
     */
    data: {
      addressList:[]
    },

    /**
     * 生命周期函数--监听页面加载
     */
    xindizhi(){
        wx.navigateTo({
          url: '../tjdizhi/tjdizhi?tjdizhi=tjdizhi',
        })
    },
    shezhimoren(e){
      let index = e.currentTarget.dataset.index
      let address = this.data.addressList[index]
      wx.put('http://localhost:3000/wx/address/default',{
        addressId:address._id,
        memberId:wx.userInfo._id,
        isDefault:address.isDefault == 1 ? 0 : 1
      }).then(res =>{
        if(!res.errorMsg){
          this.getAddressList()
        }
      })
      
    },
    xiugai(e){
      let index = e.currentTarget.dataset.index
      let address = this.data.addressList[index]
      wx.navigateTo({
        url: '../tjdizhi/tjdizhi?addressId=' + address._id,
      })
      console.log(address._id)
    },
    shanchu(e){//删除地址
      let index = e.currentTarget.dataset.index
      let address = this.data.addressList[index]
      wx.showModal({
        title:'删除提示',
        content:'确定删除此地址？',
        success:res =>{
            if(res.confirm){
              wx.delete('http://localhost:3000/wx/address',{
              addressId:address._id
            }).then(res =>{
              if(!res.errorMsg){
                this.getAddressList() //刷新列表
              }
            })
            }
        }
      })
    },
    getAddressList(){
      wx.$https.get('http://localhost:3000/wx/address').then(res=>{
        console.log(res)
        this.setData({
          addressList:res
        })
      })
    },
    onLoad: function (options) {
      this.getAddressList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady: function () {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow: function () {
      this.getAddressList()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide: function () {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload: function () {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh: function () {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom: function () {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})